package cn.edu.sjtu.se.dslab.component.design.generate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import cn.edu.sjtu.se.dslab.component.data.DomainEntity;
import cn.edu.sjtu.se.dslab.component.data.util.DataComponentUtil;
import cn.edu.sjtu.se.dslab.component.dp.parse.DesignPatternInstanceBean;
import cn.edu.sjtu.se.dslab.project.EntityZone;
import cn.edu.sjtu.se.dslab.project.EntityZoneCategory;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.service.IDomainEntityService;
import cn.edu.sjtu.se.dslab.service.IEntityZoneCategoryService;
import cn.edu.sjtu.se.dslab.service.IEntityZoneService;
import cn.edu.sjtu.se.dslab.util.FileUtil;
import cn.edu.sjtu.se.dslab.util.Tuple;
import cn.edu.sjtu.se.dslab.util.TwoTuple;


@Service("entityZoneGenerator")
public class EntityZoneGenerator extends DesignPatternCodeGenerator {
	private static final Logger logger = Logger.getLogger(EntityZoneGenerator.class);
	
	private static final String ENTITY_ZONE_TEMPLATE_NAME = "entity_zone.vm";
	private static final String ENTITY_ZONE_FILE_NAME = "entity_zone.h";

	@Resource(name = "entityZoneService")
	IEntityZoneService entityZoneService;
	
	@Resource(name = "entityZoneCategoryService")
	IEntityZoneCategoryService entityZoneCategoryService;
	
	@Resource(name = "domainEntityService")
	IDomainEntityService domainEntityService;
	
	@Resource(name = "dataComponentUtil")
	DataComponentUtil dataComponentUtil;

	public EntityZoneGenerator() {
		super();
	}

	@Override
	public void generateCode(String targetDirPath, Project project ,DesignPatternInstanceBean ...dpiBean) throws IOException {
		//List<EntityZone> entityZoneList = new ArrayList<EntityZone>();
		List<TwoTuple<EntityZone, DomainEntity>> tuples;
		
		List<Long> domainEntityIds = new ArrayList<Long>();
		for (EntityZoneCategory ezc: entityZoneCategoryService.getEntityZoneCategoryByProject(project.getId())){
			tuples = new ArrayList<TwoTuple<EntityZone, DomainEntity>>();
			for (EntityZone entityZone: entityZoneService.getEntityZoneByCategory(ezc.getId())){
				if (entityZone.getEntityUri() == null || entityZone.getEntityUri().equals("")) continue;
				DomainEntity domainEntity = domainEntityService.getDomainEntityByUri(entityZone.getEntityUri());
				if (domainEntity==null) continue; 
				//entityZone.setEntityUri(domainEntity.getName());
				TwoTuple<EntityZone, DomainEntity> tuple = Tuple.tuple(entityZone, domainEntity);
				tuples.add(tuple);
				//entityZoneList.add(entityZone);
				domainEntityIds.add(domainEntity.getId());
				logger.info(entityZone.getName());
			}
			
			inputData("tuples", tuples);
			inputData("declaration", "_" + ezc.getName().toUpperCase() + "_H_");
			StringBuilder entityZoneFile = new StringBuilder(targetDirPath);
			entityZoneFile.append("/App/").append(ezc.getName()).append(".h");
			generateCodeFile(entityZoneFile.toString(), ENTITY_ZONE_TEMPLATE_NAME);
		}
		
//		for (EntityZone entityZone: entityZoneService.getEntityZoneByProject(project.getId())){
//			if (entityZone.getEntityUri() == null || entityZone.getEntityUri().equals("")) continue;
//			DomainEntity domainEntity = domainEntityService.getDomainEntityByUri(entityZone.getEntityUri());
//			if (domainEntity==null) continue; 
//			entityZone.setEntityUri(domainEntity.getName());
//			entityZoneList.add(entityZone);
//			domainEntityIds.add(domainEntity.getId());
//			logger.info(entityZone.getName());
//		}
//		inputData("entityZones", entityZoneList);
//		StringBuilder entityZoneFile = new StringBuilder(targetDirPath);
//		entityZoneFile.append("/App/").append(ENTITY_ZONE_FILE_NAME);
//		generateCodeFile(entityZoneFile.toString(), ENTITY_ZONE_TEMPLATE_NAME);
		
		for(String path : dataComponentUtil.getDependences(domainEntityIds)){
			logger.info("Depend " + path);
			FileUtil.copyFile(path, FileUtil.getRootPath() + targetDirPath.toString() + "App/", null);// 把依赖的文件复制到生成代码的文件夹下
		}
		
		
	}
}
